<?
// ******************************************************************************
//
// create a php-image object from a file
//
// ******************************************************************************
function get_image_from_file($img_source) {
	
	// determine the image type
	$img_suffix = strtolower(substr(strrchr($img_source, "."),1));
	$img_obj = NULL;
	
	if ($img_suffix == "jpeg" || $img_suffix == "jpg" || $img_suffix == "jpe") {
		$image_obj = imagecreatefromjpeg($img_source);
	}
	/* not supported with gd versions 1.6 or later
	elseif ($img_suffix == "gif") {
		$image_obj = imagecreatefromgif($img_source);
	}
	*/
	elseif ($img_suffix == "png") {
		$image_obj = imagecreatefrompng($img_source);
	}
	else {
		return NULL;
	}
	
	if (!$image_obj)
		return NULL;
	
	return $image_obj;
}

// ******************************************************************************
//
// resize an image. limit it to the number of pixels given in max_side_length, $limited_size is either x or y
//
// ******************************************************************************
function resize_img_proportional($img_source_obj, $max_side_length, $limited_side){

	// check wich side the user wants to limit. In case of y-axis the value should be Y
	// in all other cases we limit the x-axis
	$limited_side = strtoupper($limited_side);
	if ($limited_side != "Y")
		$limited_side = "X";
	
	// determine the new measures of the image depending on the side which is to be limited
	// the image will be scaled proportionally
	$width=imagesx($img_source_obj);
	$height=imagesy($img_source_obj);
	if ($limited_side == "X"){
		$new_width = $max_side_length;
		$new_height = round($height*($max_side_length/$width));
	} else {
		$new_width = round($width*($max_side_length/$height));
		$new_height = $max_side_length;
	}
	
	if ($GLOBALS['GD_VERSION'] >=2) {
		$img_target_obj = imagecreatetruecolor ($new_width,$new_height);
		ImageCopyResampled($img_target_obj,$img_source_obj,0,0,0,0,$new_width,$new_height,$width,$height);
	}
	else {
		$img_target_obj = imagecreate($new_width,$new_height);
		imagecopyresized($img_target_obj,$img_source_obj,0,0,0,0,$new_width,$new_height,$width,$height);
	}

	return $img_target_obj;
}

// ******************************************************************************
//
// write an image to a file
// the quality paramter only has an effect on Jpeg images
//
// ******************************************************************************
function write_image_file($image_obj, $filename, $filetype, $quality) {
	$suffix = "jpg"; // default
	if (strtolower($filetype) == "png") {
		ImagePng($image_obj, $filename);
	}
	else {
		ImageJpeg($image_obj, $filename, $quality);
	}
}


function ImageCopyResampleBicubic ($dimg, $simg, $dx, $dy, $sx, $sy, $dw, $dh, $sw, $sh) {
  ImagePaletteCopy ($dimg, $simg);
  $rX = $sw / $dw;
  $rY = $sh / $dh;
  $nY = 0;
  for ($y=$dy; $y<$dh; $y++) {
   $oY = $nY;
   $nY = intval(($y + 1) * $rY+.5);
   $nX = 0;

   for ($x=$dx; $x<$dw; $x++) {
     $r = $g = $b = $a = 0;
     $oX = $nX;
     $nX = intval(($x + 1) * $rX+.5);
     for ($i=$nY; --$i>=$oY;) {
       for ($j=$nX; --$j>=$oX;) {
         $c = ImageColorsForIndex ($simg, ImageColorAt ($simg, $j, $i));
         $r += $c['red'];
         $g += $c['green'];
         $b += $c['blue'];
         $a++;
       }
     }
     ImageSetPixel ($dimg, $x, $y, ImageColorClosest ($dimg, $r/$a, $g/$a, $b/$a)); 
   }
  }
  
  return $dimg;
  
}


function get_orientation($image_params) {
	$orientation = "landscape";
	if (is_array($image_params)) {
		if ($image_params[1] > $image_params[0])
			$orientation = "portrait";
	}
	
	return $orientation;
}

function get_longest_side_pixels($image_params) {
	if (!is_array($image_params))
		return 0;
		
	if ($image_params[0] >= $image_params[1])
		return $image_params[0];
		
	return $image_params[1];
}

function process_upload_images($field_prefix, $upload_dir, $object, $id, $image_count) {
	for ($i=1; $i<=$image_count; $i++) {
	if (is_uploaded_file($_FILES[$field_prefix.$i]['tmp_name'])) {
			if( array_key_exists(get_filename_suffix($_FILES[$field_prefix.$i]['name']), $GLOBALS['ACCEPTED_IMAGE_FILES']) ){ 
				$filename = $id."_".$i."_".$_FILES[$field_prefix.$i]['name'];
				if (move_uploaded_file($_FILES[$field_prefix.$i]['tmp_name'], $upload_dir.$filename)) {
					
					// if there has been a file before and we're replacing it, delete the old one.
					eval("\$old_filename = \$object->".$field_prefix.$i.";");
					if (isset($old_filename) && $old_filename != "" && $old_filename != $filename && file_exists($upload_dir.$old_filename) ) {
						unlink ($upload_dir.$old_filename);
						unlink ($upload_dir."/thumbs/".$old_filename);
					}
					
					eval ("\$object->".$field_prefix.$i." = \$filename;");
					
					
					$image_params = getImageSize($upload_dir.$filename);
					
					do_debug("width: ".get_longest_side_pixels($image_params), 2);
					
					$orientation = get_orientation($image_params);
					$long_side = "X";
					if ($orientation == "portrait")
						$long_side = "Y";
					
					$img_obj = get_image_from_file($upload_dir.$filename);
					
					// resize the big image if it's larger than necesaray
					if (get_longest_side_pixels($image_params) > $GLOBALS['IMAGE_WIDTH']) {
						if (isset($img_obj)) {
							$img_normal = resize_img_proportional($img_obj, $GLOBALS['IMAGE_WIDTH'], $long_side);
							if (isset($img_normal)) {
								write_image_file($img_normal, $upload_dir.$filename, get_filename_suffix($filename), $GLOBALS['JPG_QUALITY']);
							}
						}
					}
					
					// create a thumbnail
					if (isset($img_obj)) {
						$img_thumb = resize_img_proportional($img_obj, $GLOBALS['THUMBNAIL_WIDTH'], $long_side);
						if (isset($img_thumb)) {
							write_image_file($img_thumb, $upload_dir."/thumbs/".$filename, get_filename_suffix($filename), $GLOBALS['JPG_QUALITY']);
						}
					}
				}
				else {
					$GLOBALS['message'] .= "Upload failed";
				}
			} else {
				$GLOBALS['message'] .= "illegal filetype";
			}
	    } else {
	       	// check if user wanted to delete the image
			if (isset($_REQUEST[$field_prefix.'del_'.$i]) && $_REQUEST[$field_prefix.'del_'.$i] == "Y") {
				eval("\$old_filename = \$object->".$field_prefix.$i.";");
				unlink ($upload_dir.$old_filename);
				unlink ($upload_dir."/thumbs/".$old_filename);
				eval("\$object->".$field_prefix.$i."=NULL;");
			}
	    }
	}
	
	return $object;

}


?>